<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of GiftbagTime_Controlers
 *
 * @author Samuel  edit by dragon.hu @2015.8.14
 */
defined('BASEPATH') or die('forbidden');

class Giftbagtime_Controller extends Controller {

    private $inc;
    private $activityList; //活动集合

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
        $this->inc = $this->config['inc'];

        //得到当前的活动
        $area_host = $_SESSION['area_selected_host'];
        $strResult = vpost($area_host . "action=10");
        $row = json_decode($strResult, true);

        foreach ($row as $key => $value) {
        	if ($value['activityId'] < 1000) //过滤礼包数据
        	{
            	$this->activityList[$value['activityId']] = $value['activityId'] . "-" . $value['name'];
        	}
        }
        //print_r($this->activityList,true);
    }

    function index() {
        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        if ($area_host == '') {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
        }
        $this->view->display("activity/giftbagtime/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();

        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        //echo " area_host:$area_host ";
        //exit();
        $strResult = vpost($area_host . "action=10");

        $row = json_decode($strResult, true);

        foreach ($row as $key => $value) {
            if ($value['activityId'] < 1000) //过滤礼包数据 
            {
            	$value['startTime'] = get_datetime($value['startTime']);
            	$value['endTime'] = get_datetime($value['endTime']);
            	$value['isOpen'] = $value['isOpen'] == 1 ? translate('OPEN') : "<label style='color:#999999'>" . translate('CLOSE') . "</label>";
            	$value['defaultstate'] = $value['defaultstate'] == 1 ? translate('YES') : translate('NO');
            	$value['isEver'] = $this->inc['activity_isever'][$value['isEver']];
            	$value['activity_type'] = $this->inc['activity_type'][$value['activity_type']];
            	$rows[] = array('id' => $value['Id'], 'cell' => $value);
            }
        }

        //$this->view->assign("activityList",self::$activityList);	//把列表传过去

        $total = count($rows);
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );

        echo json_encode($result);

        //print_r($this->activityList);
        //exit();
    }

    function add() {
        set_time_limit(30);


        if (is_submit()) {
            $rowdata = $_POST['row'];
            $area_host = $_SESSION['area_selected_host'];
            unset($rowdata['area_host']);
            $rowdata["start_time"] = strtotime($rowdata["start_time"]);
            $rowdata["end_time"] = strtotime($rowdata["end_time"]);
            $str = array_to_stringparam($rowdata);
            $str = $area_host . $str . "action=35";

            $strResult = vpost($str);

            $result = 0;
            //操作失败或者连接失败
            if ($strResult != '0' || $strResult === FALSE) {
                echo " get server data error ";
                $result = 1;
            } else {
                insert_log(translate('MENU_ACTIVITY_ACTIVITYTIME'),translate('ADD'), NULL, array("name" => $area_host . ' activity: ' . $rowdata['activity_id']));
            }
            cp_tips($result == 0 ? "success" : "error");
        }
        //var_dump($this->activityList); 
        $this->view->assign("activityList", $this->activityList); //把列表传过去        
        $this->view->display("activity/giftbagtime/add");
    }

    function edit($id = 0) {
        if (is_submit()) {
            $rowdata = $_POST['row'];
            //print_r($rowdata);
            //exit(0);
            
            $rowdata['level'] = 1;
            $rowdata['times'] = 1;
            $rowdata['defaultstate'] = 0;
            $rowdata['subid'] = 1;  //礼包暂时把subid置为1
            $rowdata['sequence'] = 0;  //礼包无所谓顺序
            $rowdata['startTime'] = strtotime($rowdata['startTime']);
            $rowdata['endTime'] = strtotime($rowdata['endTime']);
            $rowdata['dayreset'] = 0;  //礼包无所谓重置时间
            if ($_POST['Operate'] == 0) {
                $area_host = get_area_list_url();
                $area_live_list = get_area_list_live();
                foreach ($area_live_list as $k => $w) {  //echo $k."***";
                    $strResult = vpost($area_host[$k] . array_to_stringparam($rowdata) . "action=11&id={$id}", 2);
                }
            } else if ($_POST['Operate'] == 1) {
                $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
                //echo $area_host . array_to_stringparam($rowdata) . "action=11&id={$id}";exit(0);
                $strResult = vpost($area_host . array_to_stringparam($rowdata) . "action=11&id={$id}", 2);
            }

            $result = 0;
            if ($strResult === FALSE || $strResult != '0') {
                $result = 1;
            }
            //echo $area_host . array_to_stringparam($rowdata) . "action=11&id={$id}";
            cp_tips($result == 0 ? "success" : "error");
        }
        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        $strResult = vpost($area_host . "action=10&id={$id}");
        $row = json_decode($strResult, true);
        /* echo $area_host . "action=10&id={$id}"."<br>";
          print_r($row); */
        //print_r($row);
        //exit(0);
        $this->view->assign("row", $row[0]);
        $this->view->assign("isEver", $this->form->select("row[isEver]", $this->inc['activity_isever'], $row[0]['isEver'], array('id' => 'isEver')));
        $this->view->display("activity/giftbagtime/edit");
    }

    function copyToOther($ids = 0) {
    	print_r($_POST);
    	echo " $ids = $ids ";
        if (is_submit()) {
        	/*
            $rowdata = $_POST['row'];

            $rowdata['startTime'] = strtotime($rowdata['startTime']);
            $rowdata['endTime'] = strtotime($rowdata['endTime']);
            if ($_POST['Operate'] == 0) {
                $area_host = get_area_list_url();
                $area_live_list = get_area_list_live();
                foreach ($area_live_list as $k => $w) {  //echo $k."***";
                    $strResult = vpost($area_host[$k] . array_to_stringparam($rowdata) . "action=11&id={$id}", 2);
                }
            } else if ($_POST['Operate'] == 1) {
                $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
                //echo $area_host . array_to_stringparam($rowdata) . "action=11&id={$id}";exit;
                $strResult = vpost($area_host . array_to_stringparam($rowdata) . "action=11&id={$id}", 2);
            }

            $result = 0;
            if ($strResult === FALSE || $strResult != '0') {
                $result = 1;
            }
            //echo $area_host . array_to_stringparam($rowdata) . "action=11&id={$id}";
            cp_tips($result == 0 ? "success" : "error");
            */
        }
        //$area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        //$strResult = vpost($area_host . "action=10&id={$ids}");
        //$row = json_decode($strResult, true);
        /* echo $area_host . "action=10&id={$id}"."<br>";
          print_r($row); */
        $this->view->assign("ids",json_decode($ids));
        $this->view->assign("row", $row[0]);
        $this->view->display("activity/giftbagtime/copy");
    }

}

?>
